|
1
|
|
|
/* |
|
2
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5
|
|
|
* you may not use this file except in compliance with the License. |
|
6
|
|
|
* You may obtain a copy of the License at |
|
7
|
|
|
* |
|
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
* |
|
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
* See the License for the specific language governing permissions and |
|
14
|
|
|
* limitations under the License. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
goog.provide('twig.Environment'); |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
goog.require('twig'); |
|
20
|
|
|
goog.require('twig.ExtensionInterface'); |
|
21
|
|
|
goog.require('twig.Template'); |
|
22
|
|
|
goog.require('twig.filter'); |
|
23
|
|
|
|
|
24
|
|
|
goog.require('goog.object'); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @constructor |
|
28
|
|
|
*/ |
|
29
|
|
|
twig.Environment = function() { |
|
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @private |
|
32
|
|
|
* @type {Object.<twig.ExtensionInterface>} |
|
33
|
|
|
*/ |
|
34
|
|
|
this.extensions_ = {}; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @private |
|
38
|
|
|
* @type {Object.<Function>} |
|
39
|
|
|
*/ |
|
40
|
|
|
this.filters_ = {}; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @private |
|
44
|
|
|
* @type {Object.<Function>} |
|
45
|
|
|
*/ |
|
46
|
|
|
this.functions_ = {}; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @private |
|
50
|
|
|
* @type {Object.<Function>} |
|
51
|
|
|
*/ |
|
52
|
|
|
this.tests_ = {}; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @private |
|
56
|
|
|
* @type {Object.<twig.Template>} |
|
57
|
|
|
*/ |
|
58
|
|
|
this.createdTemplates_ = {}; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @private |
|
62
|
|
|
* @type {Object} |
|
63
|
|
|
*/ |
|
64
|
|
|
this.globals_ = {}; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @private |
|
68
|
|
|
* @type {boolean} |
|
69
|
|
|
*/ |
|
70
|
|
|
this.runtimeInitialized = false; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @private |
|
74
|
|
|
* @type {string} |
|
75
|
|
|
*/ |
|
76
|
|
|
this.charset_ = 'UTF-8'; |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the rendered template. |
|
81
|
|
|
* |
|
82
|
|
|
* @export |
|
83
|
|
|
* @param {Function} ctor The constructor of the template |
|
84
|
|
|
* @param {Object.<*>=} opt_context |
|
85
|
|
|
* @return {string} |
|
86
|
|
|
*/ |
|
87
|
|
|
twig.Environment.prototype.render = function(ctor, opt_context) { |
|
|
|
|
|
|
88
|
|
|
var template = this.createTemplate(ctor); |
|
89
|
|
|
|
|
90
|
|
|
return template.render.call(template, twig.extend({}, this.globals_, opt_context || {})); |
|
|
|
|
|
|
91
|
|
|
}; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Delegates to a filter function at runtime. |
|
95
|
|
|
* |
|
96
|
|
|
* @param {string} name |
|
97
|
|
|
* @param {*} arg1 |
|
98
|
|
|
* @param {...*} var_args |
|
99
|
|
|
* @return {*} |
|
100
|
|
|
*/ |
|
101
|
|
|
twig.Environment.prototype.filter = function(name, arg1, var_args) { |
|
|
|
|
|
|
102
|
|
|
if (!goog.object.containsKey(this.filters_, name)) { |
|
|
|
|
|
|
103
|
|
|
throw Error("The filter '" + name + "' does not exist."); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return this.filters_[name].apply(null, Array.prototype.slice.call(arguments, 1)); |
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Delegates to a function at runtime. |
|
111
|
|
|
* |
|
112
|
|
|
* @param {string} name |
|
113
|
|
|
* @param {*} arg1 |
|
114
|
|
|
* @param {...*} var_args |
|
115
|
|
|
* @return {*} |
|
116
|
|
|
*/ |
|
117
|
|
|
twig.Environment.prototype.invoke = function(name, arg1, var_args) { |
|
|
|
|
|
|
118
|
|
|
if (!goog.object.containsKey(this.functions_, name)) { |
|
|
|
|
|
|
119
|
|
|
throw Error("The function '" + name + "' does not exist."); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return this.functions_[name].apply(null, Array.prototype.slice.call(arguments, 1)); |
|
123
|
|
|
}; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Delegates to a test function at runtime. |
|
127
|
|
|
* |
|
128
|
|
|
* @param {string} name |
|
129
|
|
|
* @param {*} arg1 |
|
130
|
|
|
* @param {...*} var_args |
|
131
|
|
|
* @return {boolean} |
|
132
|
|
|
*/ |
|
133
|
|
|
twig.Environment.prototype.test = function(name, arg1, var_args) { |
|
|
|
|
|
|
134
|
|
|
if (!goog.object.containsKey(this.tests_, name)) { |
|
|
|
|
|
|
135
|
|
|
throw Error("The test '" + name + "' does not exist."); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return /** @type {boolean} */ ( |
|
139
|
|
|
this.tests_[name].apply(null, Array.prototype.slice.call(arguments, 1))); |
|
140
|
|
|
}; |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Escapes the given value for the given context. |
|
144
|
|
|
* |
|
145
|
|
|
* @param {string} value |
|
146
|
|
|
* @param {twig.filter.escape.Type=} opt_type |
|
147
|
|
|
* @param {string=} opt_charset |
|
148
|
|
|
* @param {boolean=} opt_autoescape |
|
149
|
|
|
* @return {string} |
|
150
|
|
|
*/ |
|
151
|
|
|
twig.Environment.prototype.escape = function(value, opt_type, opt_charset, opt_autoescape) { |
|
|
|
|
|
|
152
|
|
|
return twig.filter.escape(this, value, opt_type, opt_charset, opt_autoescape); |
|
|
|
|
|
|
153
|
|
|
}; |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Delegates to a macro at runtime. |
|
157
|
|
|
* |
|
158
|
|
|
* @param {Function} templateCtor |
|
159
|
|
|
* @param {string} macroName |
|
160
|
|
|
* @param {...*} var_args |
|
161
|
|
|
* @return {string} |
|
162
|
|
|
*/ |
|
163
|
|
|
twig.Environment.prototype.macro = function(templateCtor, macroName, var_args) { |
|
|
|
|
|
|
164
|
|
|
var template = this.createTemplate(templateCtor); |
|
165
|
|
|
var macro = template['get' + macroName]; |
|
166
|
|
|
|
|
167
|
|
|
if (!macro) { |
|
168
|
|
|
throw Error("The macro '" + macroName + "' does not exist on template '" + template.getTemplateName() + "'."); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return macro.apply(template, Array.prototype.slice.call(arguments, 2)).toString(); |
|
172
|
|
|
}; |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Sets a dynamic filter function at runtime. |
|
176
|
|
|
* |
|
177
|
|
|
* @export |
|
178
|
|
|
* @param {string} name |
|
179
|
|
|
* @param {Function} filter |
|
180
|
|
|
*/ |
|
181
|
|
|
twig.Environment.prototype.setFilter = function(name, filter) { |
|
|
|
|
|
|
182
|
|
|
this.filters_[name] = filter; |
|
183
|
|
|
}; |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets a dynamic function at runtime |
|
187
|
|
|
* |
|
188
|
|
|
* @export |
|
189
|
|
|
* @param {string} name |
|
190
|
|
|
* @param {Function} func |
|
191
|
|
|
*/ |
|
192
|
|
|
twig.Environment.prototype.setFunction = function(name, func) { |
|
|
|
|
|
|
193
|
|
|
this.functions_[name] = func; |
|
194
|
|
|
}; |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Sets a dynamic test function at runtime |
|
198
|
|
|
* |
|
199
|
|
|
* @export |
|
200
|
|
|
* @param {string} name |
|
201
|
|
|
* @param {Function} func |
|
202
|
|
|
*/ |
|
203
|
|
|
twig.Environment.prototype.setTest = function(name, func) { |
|
|
|
|
|
|
204
|
|
|
this.tests_[name] = func; |
|
205
|
|
|
}; |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Sets the global variables. |
|
209
|
|
|
* |
|
210
|
|
|
* @export |
|
211
|
|
|
* @param {Object} globals |
|
212
|
|
|
*/ |
|
213
|
|
|
twig.Environment.prototype.setGlobals = function(globals) { |
|
|
|
|
|
|
214
|
|
|
this.globals_ = globals; |
|
215
|
|
|
}; |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Sets a single global variable. |
|
219
|
|
|
* |
|
220
|
|
|
* @export |
|
221
|
|
|
* @param {string} key |
|
222
|
|
|
* @param {*} value |
|
223
|
|
|
*/ |
|
224
|
|
|
twig.Environment.prototype.setGlobal = function(key, value) { |
|
|
|
|
|
|
225
|
|
|
this.globals_[key] = value; |
|
226
|
|
|
}; |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return {Object} |
|
230
|
|
|
*/ |
|
231
|
|
|
twig.Environment.prototype.getGlobals = function() { |
|
|
|
|
|
|
232
|
|
|
return this.globals_; |
|
233
|
|
|
}; |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return {string} |
|
237
|
|
|
*/ |
|
238
|
|
|
twig.Environment.prototype.getCharset = function() { |
|
|
|
|
|
|
239
|
|
|
return this.charset_; |
|
240
|
|
|
}; |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param {string} charset |
|
244
|
|
|
*/ |
|
245
|
|
|
twig.Environment.prototype.setCharset = function(charset) { |
|
|
|
|
|
|
246
|
|
|
this.charset_ = charset; |
|
247
|
|
|
}; |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Initializes the runtime environment. |
|
251
|
|
|
*/ |
|
252
|
|
|
twig.Environment.prototype.initRuntime = function() { |
|
|
|
|
|
|
253
|
|
|
this.runtimeInitialized = true; |
|
254
|
|
|
|
|
255
|
|
|
goog.object.forEach(this.getExtensions(), function(extension) { |
|
|
|
|
|
|
256
|
|
|
extension.initRuntime(this); |
|
257
|
|
|
}, this); |
|
258
|
|
|
}; |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Returns true if the given extension is registered. |
|
262
|
|
|
* |
|
263
|
|
|
* @param {string} name The extension name |
|
264
|
|
|
* |
|
265
|
|
|
* @return {boolean} Whether the extension is registered or not |
|
266
|
|
|
*/ |
|
267
|
|
|
twig.Environment.prototype.hasExtension = function(name) { |
|
|
|
|
|
|
268
|
|
|
return goog.object.containsKey(this.extensions_, name); |
|
|
|
|
|
|
269
|
|
|
}; |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Gets an extension by name. |
|
273
|
|
|
* |
|
274
|
|
|
* @param {string} name The extension name |
|
275
|
|
|
* |
|
276
|
|
|
* @return {twig.ExtensionInterface} |
|
277
|
|
|
*/ |
|
278
|
|
|
twig.Environment.prototype.getExtension = function(name) { |
|
|
|
|
|
|
279
|
|
|
if (!goog.object.containsKey(this.extensions_, name)) { |
|
|
|
|
|
|
280
|
|
|
throw Error('The "' + name + '" extension is not enabled.'); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return this.extensions_[name]; |
|
284
|
|
|
}; |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Registers an extension. |
|
288
|
|
|
* |
|
289
|
|
|
* @param {twig.ExtensionInterface} extension |
|
290
|
|
|
*/ |
|
291
|
|
|
twig.Environment.prototype.addExtension = function(extension) { |
|
|
|
|
|
|
292
|
|
|
this.extensions_[extension.getName()] = extension; |
|
293
|
|
|
}; |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Removes an extension by name. |
|
297
|
|
|
* |
|
298
|
|
|
* @param {string} name The extension name |
|
299
|
|
|
*/ |
|
300
|
|
|
twig.Environment.prototype.removeExtension = function(name) { |
|
|
|
|
|
|
301
|
|
|
delete this.extensions_[name]; |
|
302
|
|
|
}; |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Registers an array of extensions. |
|
306
|
|
|
* |
|
307
|
|
|
* @param {Array.<twig.ExtensionInterface>} extensions An array of extensions |
|
308
|
|
|
*/ |
|
309
|
|
|
twig.Environment.prototype.setExtensions = function(extensions) { |
|
|
|
|
|
|
310
|
|
|
goog.object.forEach(extensions, function(extension) { |
|
|
|
|
|
|
311
|
|
|
this.addExtension(extension); |
|
312
|
|
|
}); |
|
313
|
|
|
}; |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Returns all registered extensions. |
|
317
|
|
|
* |
|
318
|
|
|
* @return {Object.<string, twig.ExtensionInterface>} An array of extensions |
|
319
|
|
|
*/ |
|
320
|
|
|
twig.Environment.prototype.getExtensions = function() { |
|
|
|
|
|
|
321
|
|
|
return this.extensions_; |
|
322
|
|
|
}; |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param {Function} ctor |
|
326
|
|
|
* @return {twig.Template} |
|
327
|
|
|
*/ |
|
328
|
|
|
twig.Environment.prototype.createTemplate = function(ctor) { |
|
|
|
|
|
|
329
|
|
|
var uid = goog.getUid(ctor); |
|
|
|
|
|
|
330
|
|
|
|
|
331
|
|
|
if (goog.object.containsKey(this.createdTemplates_, uid)) { |
|
332
|
|
|
return this.createdTemplates_[uid]; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
if (false === this.runtimeInitialized) { |
|
336
|
|
|
this.initRuntime(); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
var template = /** @type {twig.Template} */ (new ctor(this)); |
|
|
|
|
|
|
340
|
|
|
|
|
341
|
|
|
this.createdTemplates_[uid] = template; |
|
342
|
|
|
|
|
343
|
|
|
return template; |
|
344
|
|
|
}; |
|
345
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.